home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
Book Chapters
/
11 - Porting
/
Sample Code
/
ErrCheck.c
< prev
next >
Wrap
Text File
|
1995-03-28
|
4KB
|
154 lines
/************************************************************************************
* *
* ErrCheck.c *
* *
* ©1995 Douglas Grounds. All Rights Reserved. *
* *
* This file contains error checking functions which makes checking for resource, *
* memory and other error conditions much simpler. Examples are given for the *
* use of each function. *
* *
************************************************************************************/
#include <Dialogs.h>
#include <Memory.h>
#include <Resources.h>
#include <SegLoad.h>
#include <stdio.h>
#include <strings.h>
#include <Types.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#include "ErrCheck.h"
/*-----------------------------------
*
* Constants
*
*-----------------------------------*/
#define kErrAlertId 128
/*-----------------------------------
*
* Example Usage
*
*-----------------------------------*/
#if (FALSE)
void myExampleFunction (void)
{
Handle h;
long count;
short err, myFRef;
h = GetResource('DATA', 128);
if (checkResError(h))
{
notifyOfError("GetResource", "MyExampleFunction", ResError(), FALSE);
return;
}
... use "h" ...
h = NewHandle( 1024 );
if (checkMemError(h))
{
notifyOfError("NewHandle", "MyExampleFunction", MemError(), FALSE);
return;
}
... use "h" ...
... assign myFRef, count
... lock down "h"
err = FSWrite(myFRef, &count, *h);
if (checkOSErr(err))
{
notifyOfError("FSWrite", "MyExampleFunction", err, FALSE);
return;
}
}
#endif
/******************************************************
* notifyOfError. *
* *
* Displays an alert that informs the user that *
* a function has returned an error and where the *
* call to that function was made. *
******************************************************/
void notifyOfError (char *routineName, char *caller, short errValue, Boolean isFatal)
{
Str255 localStr;
// Prepare the text.
sprintf((char *)localStr, "The function ‘%s’ (called by ‘%s’) returned an error. The error number was: %d.",
routineName, caller, errValue);
c2pstr((char *)localStr);
// Tell the user!
ParamText(localStr, "\p", "\p", "\p");
(void) Alert(kErrAlertId, NULL);
// If fatal error, then quit!
if (isFatal)
{
// May need to perform shut-down functions
ExitToShell();
}
}
/******************************************************
* checkResError. *
* *
* If either "h" is NULL, or ResError() != noErr, *
* return TRUE. *
******************************************************/
Boolean checkResError (Handle h)
{
return ((h == NULL) || ResError());
}
/******************************************************
* checkOSErr. *
* *
* Checks the value of "err" and returns TRUE if an *
* error has occurred. Could be implemented as a *
* macro. *
******************************************************/
Boolean checkOSErr (OSErr err)
{
return (err != noErr);
}
/******************************************************
* checkMemError. *
* *
* Checks both the value of "myPointerOrHandle" and *
* the value of MemError. Returns TRUE if an error *
* has occurred. *
******************************************************/
Boolean checkMemError (void *myPointerOrHandle)
{
return ((myPointerOrHandle == NULL) || MemError());
}